home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / SearchReplace.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  3.0 KB  |  99 lines

  1. //: C21:SearchReplace.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // The STL search and replace algorithms
  7. #include "PrintSequence.h"
  8. #include <vector>
  9. #include <algorithm>
  10. #include <functional>
  11. using namespace std;
  12.  
  13. struct PlusOne {
  14.   bool operator()(int i, int j) {
  15.     return j == i + 1;
  16.   }
  17. };
  18.  
  19. class MulMoreThan {
  20.   int value;
  21. public:
  22.   MulMoreThan(int val) : value(val) {}
  23.   bool operator()(int v, int m) {
  24.     return v * m > value;
  25.   }
  26. };
  27.  
  28. int main() {
  29.   int a[] = { 1, 2, 3, 4, 5, 6, 6, 7, 7, 7,
  30.     8, 8, 8, 8, 11, 11, 11, 11, 11 };
  31.   const int asz = sizeof a / sizeof *a;
  32.   vector<int> v(a, a + asz);
  33.   print(v, "v", " ");
  34.   vector<int>::iterator it =
  35.     find(v.begin(), v.end(), 4);
  36.   cout << "find: " << *it << endl;
  37.   it = find_if(v.begin(), v.end(), 
  38.     bind2nd(greater<int>(), 8));
  39.   cout << "find_if: " << *it << endl;
  40.   it = adjacent_find(v.begin(), v.end());
  41.   while(it != v.end()) {
  42.     cout << "adjacent_find: " << *it 
  43.       << ", " << *(it + 1) << endl;
  44.     it = adjacent_find(it + 2, v.end());
  45.   }
  46.   it = adjacent_find(v.begin(), v.end(), 
  47.     PlusOne());
  48.   while(it != v.end()) {
  49.     cout << "adjacent_find PlusOne: " << *it
  50.       << ", " << *(it + 1) << endl;
  51.     it = adjacent_find(it + 1, v.end(), 
  52.       PlusOne());
  53.   }
  54.   int b[] = { 8, 11 };
  55.   const int bsz = sizeof b / sizeof *b;
  56.   print(b, b + bsz, "b", " ");
  57.   it = find_first_of(v.begin(), v.end(),
  58.     b, b + bsz);
  59.   print(it, it + bsz, "find_first_of", " ");
  60.   it = find_first_of(v.begin(), v.end(), 
  61.     b, b + bsz, PlusOne());
  62.   print(it,it + bsz,"find_first_of PlusOne"," ");
  63.   it = search(v.begin(), v.end(), b, b + bsz);
  64.   print(it, it + bsz, "search", " ");
  65.   int c[] = { 5, 6, 7 };
  66.   const int csz = sizeof c / sizeof *c;
  67.   print(c, c + csz, "c", " ");
  68.   it = search(v.begin(), v.end(), 
  69.     c, c + csz, PlusOne());
  70.   print(it, it + csz,"search PlusOne", " ");
  71.   int d[] = { 11, 11, 11 };
  72.   const int dsz = sizeof d / sizeof *d;
  73.   print(d, d + dsz, "d", " ");
  74.   it = find_end(v.begin(), v.end(), d, d + dsz);
  75.   print(it, v.end(),"find_end", " ");
  76.   int e[] = { 9, 9 };
  77.   print(e, e + 2, "e", " ");
  78.   it = find_end(v.begin(), v.end(), 
  79.     e, e + 2, PlusOne());
  80.   print(it, v.end(),"find_end PlusOne"," ");
  81.   it = search_n(v.begin(), v.end(), 3, 7);
  82.   print(it, it + 3, "search_n 3, 7", " ");
  83.   it = search_n(v.begin(), v.end(), 
  84.     6, 15, MulMoreThan(100));
  85.   print(it, it + 6, 
  86.     "search_n 6, 15, MulMoreThan(100)", " ");
  87.   cout << "min_element: " <<
  88.     *min_element(v.begin(), v.end()) << endl;
  89.   cout << "max_element: " <<
  90.     *max_element(v.begin(), v.end()) << endl;
  91.   vector<int> v2;
  92.   replace_copy(v.begin(), v.end(), 
  93.     back_inserter(v2), 8, 47);
  94.   print(v2, "replace_copy 8 -> 47", " ");
  95.   replace_if(v.begin(), v.end(), 
  96.     bind2nd(greater_equal<int>(), 7), -1);
  97.   print(v, "replace_if >= 7 -> -1", " ");
  98. } ///:~
  99.